const api = require("../../request/api")
const request = require("../../request/request")
const util = require("../../utils/util")
const App = getApp();

// pages/cash/cash.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        barHeight: App.globalData.paddingTop,
        // 导航栏数据
        styledata: {
            "bg_color": "#fff",
            "color": "#000",
            "name": "零钱中心",
            "leftflag": true,
            // "homeflag": true,
        },

        // ts组件对话框的 确认取消按钮
        confirmBtn: { content: '前往认证', variant: 'base' },
        isShowjump: false,

        showIdentify: false,
        showBindbank: false,

        // 是否正在实名认证
        Identiting: false,

        // 实名认证模态框y轴移动距离
        idenmoveNum: '-205%',
        // 银行卡绑定模态框y轴移动距离
        cardmoveNum: '-205%',

        uid: '',
        userInfo: {},
        isVip: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    async onLoad(options) {
        await this.initData(options)
        this.setData({
            ['userInfo.allin_moblie']: '13824817359'
        })
    },

    async initData(options) {
        await App.getMyFullInfo()
        this.setData({
            userInfo: App.globalData.fulluserInfo,
            isVip: App.globalData.isVip,
            uid: App.globalData.uid
        })
    },

    // 检测是否为分销员
    checkIsDistributor() {
        let allinMoblie = this.data.userInfo.allin_moblie
        let isVip = this.data.isVip
        let isdistri = allinMoblie.length > 0 && isVip ? true : false
        return isdistri
    },

    confirmDialog() {
        this.setData({ isShowjump: false });
        wx.navigateTo({
            url: '/distribution/distributor/distributor',
            success: (result) => {

            },
            fail: () => { },
            complete: () => { }
        });

    },

    closeDialog() {
        this.setData({ isShowjump: false });
        // wx.navigateBack({
        //     delta: 1
        // });

    },

    // 提现回调
    goWithDraw() {
        let isDistri = this.checkIsDistributor()
        // console.log("isDistri", isDistri);
        // console.log('提现');

        // 非分销员，弹窗提示前往认证页面
        if (!isDistri) {
            this.setData({
                isShowjump: true
            })
        }
        // 依次验证实名、银行卡绑定
        else {
            let isIdentity = this.data.userInfo.allin_identity
            let hasBankCard = this.data.userInfo.allin_bank_num
            // console.log('isIdentity', isIdentity);
            // console.log('hasBankCard', hasBankCard);

            if (isIdentity != 1) {
                this.showIdenModal()
                // this.operateIdentity()
            }
            // 已实名且无银行卡绑定
            if (isIdentity == 1 && !hasBankCard) {
                this.showCardModal()
            }
        }
    },

    // 点击认证按钮回调
    async confirmIdentity() {
        let result = await this.sendIdentity()
        // console.log('confirmIdentity', result);
        if (result) {
            this.closeIdentity()
            await this.initData()

            this.setData({
                ['userInfo.allin_identity']: '1',
                ['userInfo.allin_moblie']: '13824817359'
            })

            // 手动调用提现功能 自动判断拉起银行卡绑定
            this.goWithDraw()
        } else {
            util.showToast('实名认证失败')
        }
    },


    // 弹出实名认证模态框
    showIdenModal() {
        // 出修改框
        this.setData({
            idenmoveNum: '-50%'
        })
    },

    // 弹出银行卡绑定模态框
    showCardModal() {
        // 出修改框
        this.setData({
            cardmoveNum: '-50%'
        })
    },

    // 取消修改 关闭模态框
    closeIdentity() {
        this.setData({
            idenmoveNum: '-205%',
            idenName: '',
            idenCardNum: ''
        })
    },

    inputBlur(e) {
        let valname = e.currentTarget.dataset.valname
        this.setData({
            [`${valname}`]: e.detail.value
        })
    },

    // 发送实名认证请求
    async sendIdentity() {
        this.setData({
            Identiting: true
        })
        console.log("uid", this.data.uid);
        console.log("this.data.idenName", this.data.idenName);
        console.log("this.data.idenCardNum", this.data.idenCardNum);


        return new Promise((resolve, reject) => {
            setTimeout(() => {
                this.setData({
                    Identiting: false,
                })
                resolve(true)
                // resolve(false)
            }, 5000);
        })

    },


    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})